<fieldset>
	<legend>Danh sách giao dịch</legend>
        <form class="well form-search">   
            <div class="right">
                <input type="text" name="q" data-provide="typeahead" class="input-medium search-query" autocomplete="off" placeholder="Họ tên"/>
                <input text="text" name="date" class="input-medium" placeholder="Ngày tháng" data-datepicker />       
                <select name="type" class="input-medium">
                    <option value="0">Tất cả trạng thái</option>
                    <option value="1"><?php echo Model_Transaction::typeToString(1); ?></option>
                    <option value="2"><?php echo Model_Transaction::typeToString(2); ?></option>
                    <option value="3"><?php echo Model_Transaction::typeToString(3); ?></option>
                </select>
                <button type="submit" class="btn">Tìm kiếm</button>
            </div>
        </form>
	<table class="table table-striped">
        <tr>        
            <th>Ngày tháng</th>
            <th>Họ tên</th>
            <th>Tựa sách</th>
            <th>Trạng thái</th>   
            <th>&nbsp;</th>
        </tr>
        <?php foreach($this->transactions as $transaction) { ?>
        <tr id="<?php echo $transaction['TransactionID']; ?>">
            <td><?php echo date('d/m/Y H:i:s', $transaction['CreateDate']); ?></td>
            <td><?php echo $transaction['AccountName']; ?></td>
            <td><?php echo $transaction['BookTitle']; ?></td>
            <td><?php echo Model_Transaction::typeToString($transaction['Type']); ?></td> 
            <td><select name="type" class="input-medium" onchange="Transaction.update(<?php echo $transaction['TransactionID']; ?>, this.value);">
                    <option value="0">Cập nhật trạng thái...</option>
                    <option value="1"><?php echo Model_Transaction::typeToString(1); ?></option>
                    <option value="2"><?php echo Model_Transaction::typeToString(2); ?></option>
                    <option value="3"><?php echo Model_Transaction::typeToString(3); ?></option>
                </select> / <a href="javascript:;" onclick="if (confirm('Bạn có chắc chắn muốn xoá giao dịch này?')) Transaction.delete(<?php echo $transaction['TransactionID']; ?>);">Xoá</a></td>
        </tr>
        <?php } ?>
    </table>
    <?php echo $this->pagination; ?>
</fieldset>
<script type="text/javascript">
    
    $(function(){
        $('.search-query').typeahead({
            source : <?php echo $this->accounts; ?>
        });
    })    
    
    var Transaction = {
	'update' : function(TransactionID, Type){		
            $.ajax({
                url: config.base_path + '/librarian/transaction/edit',
                type: 'post',
                data: {
                    TransactionID: TransactionID,
                    Type: Type
                },
                dataType: 'json',					
                success: function(res){
                    if (res.status == 0)
                    {
                        Utils.Message.Error(res.message);
                    }         
                    else
                    {
                        window.location.reload();
                    }
                }
            });
	},
        'delete' : function(TransactionID){
            $.ajax({
                url: config.base_path + '/librarian/transaction/delete',
                type: 'post',
                data: {
                    TransactionID: TransactionID                   
                },
                dataType: 'json',					
                success: function(res){
                    if (res.status == 0)
                    {
                        Utils.Message.Error(res.message);
                    }         
                    else
                    {
                        window.location.reload();                      
                    }
                }
            });
        }
    };
</script>